package com.henry.db.migrate.util;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;

import javax.xml.datatype.DatatypeConfigurationException;
import javax.xml.datatype.DatatypeFactory;
import javax.xml.datatype.XMLGregorianCalendar;

public class DateUtil
{
  public static String defaultFormate = "yyyy-MM-dd HH:mm:ss,SSS"; //榛樿鐨勬棩鏈熸牸寮�

  /***
   * @param paramDate 鏃ユ湡
   * @param paramInt1 0-6 姣銆佺銆佸垎銆佹椂銆佹棩銆佹湀銆佸勾
   * @param paramInt2 澧為噺 涓烘暣鍨�
   * ***/
  public static final Date dateAdd(Date paramDate, int paramInt1, int paramInt2)
  {
    if (paramDate == null)
      return null;
    Calendar localCalendar = Calendar.getInstance();
    localCalendar.setTime(paramDate);
    switch (paramInt1)
    {
    case 0:
      localCalendar.add(14, paramInt2);
      break;
    case 1:
      localCalendar.add(13, paramInt2);
      break;
    case 2:
      localCalendar.add(12, paramInt2);
      break;
    case 3:
      localCalendar.add(11, paramInt2);
      break;
    case 4:
      localCalendar.add(5, paramInt2);
      break;
    case 5:
      localCalendar.add(2, paramInt2);
      break;
    case 6:
      localCalendar.add(1, paramInt2);
    }
    return new Date(localCalendar.getTimeInMillis());
  }

  /**
   * 杩斿洖涓�釜浠ｈ〃褰撴湡鏃ユ湡鏄槦鏈熷嚑鐨勬暟瀛椼�1琛ㄧず鏄熸湡澶┿�2琛ㄧず鏄熸湡涓��7琛ㄧず鏄熸湡鍏�
   * **/
  public static int getDayOfWeek(String year, String month, String day)
  {
    Calendar cal = new GregorianCalendar(new Integer(year).intValue(), new Integer(month).intValue() - 1, new Integer(day).intValue());
    return cal.get(Calendar.DAY_OF_WEEK);
  }

  /**
   * 褰撳墠鏃堕棿鏄惁绗﹀悎鏃堕棿琛ㄨ揪寮�
   * @param cron
   * @return
   * @throws ParseException
   */

  /**
   * 榛樿鏍煎紡鏍煎紡鍖栧綋鍓嶆椂闂�
   * @return
   */
  public static String formatNow()
  {
    return DateUtil.formatNow(defaultFormate);
  }

  /**
   * 鎸夋寚瀹氭牸寮忔牸寮忓寲褰撳墠鏃堕棿
   * @return
   */
  public static String formatNow(String pattern)
  {
    DateFormat dateFormat = new SimpleDateFormat(pattern);
    return dateFormat.format(new Date());
  }

  /**
   * 榛樿鏍煎紡鏍煎紡鍖栧嚑鍒嗛挓鍚庣殑鏃堕棿
   * @param min
   * @return
   */
  public static String formatMinutesLater(int min)
  {
    Calendar c = Calendar.getInstance();
    Date date = new Date();
    c.setTime(date);
    c.add(Calendar.MINUTE, min);
    DateFormat dateFormat = new SimpleDateFormat(defaultFormate);
    return dateFormat.format(c.getTime());
  }

  /**
   * 鎸夋寚瀹氭牸寮忔牸寮忓寲鍑犲垎閽熷悗鐨勬椂闂�
   * @param min
   * @return
   */
  public static String formatMinutesLater(String pattern, int min)
  {
    Calendar c = Calendar.getInstance();
    Date date = new Date();
    c.setTime(date);
    c.set(Calendar.MINUTE, c.get(Calendar.MINUTE) + min);
    DateFormat dateFormat = new SimpleDateFormat(pattern);
    return dateFormat.format(c.getTime());
  }

  /**
   * 鎸夐粯璁ゆ牸寮忔牸寮忓寲鏃ユ湡
   * @param date
   * @return
   */
  public static String format(Date date)
  {
    return DateUtil.format(date, defaultFormate);
  }

  /**
   * 鎸夋寚瀹氭牸寮忔牸寮忓寲鏃ユ湡
   * @param date
   * @return
   */
  public static String format(Date date, String pattern)
  {
    DateFormat dateFormat = new SimpleDateFormat(pattern);
    if (date != null)
      return dateFormat.format(date);
    else
      return null;
  }

  /**
   * 鏍煎紡鍖朮ML鏃堕棿涓哄瓧绗︿覆
   * @param xgc
   * @param pattern
   * @return
   */
  public static String format(XMLGregorianCalendar xgc, String pattern)
  {
    Date date = parseDate(xgc);
    return DateUtil.format(date, pattern);
  }

  /**
   * 鎸囧畾鏍煎紡鐨勬棩鏈熻浆鍖栦负XML鏃堕棿
   * @param time
   * @param pattern
   * @return
   * @throws ParseException
   * @throws DatatypeConfigurationException
   */

  /**
   * Date杞寲涓篨MLGregorianCalendar
   * @param date
   * @return
   * @throws ParseException
   * @throws DatatypeConfigurationException
   */
  public static XMLGregorianCalendar parseXMLGregorianCalendar(Date date)
      throws ParseException, DatatypeConfigurationException
  {
    GregorianCalendar gc = new GregorianCalendar();
    gc.setTime(date);
    XMLGregorianCalendar xgc = DatatypeFactory.newInstance().newXMLGregorianCalendar(gc);
    return xgc;
  }

  /**
   * 鏁版嵁搴揇ate绫诲瀷杞寲涓篨ML鏃堕棿
   * @param date
   * @return
   * @throws DatatypeConfigurationException
   */
  public XMLGregorianCalendar parseXMLGregorianCalendar(java.sql.Date date)
      throws DatatypeConfigurationException
  {
    GregorianCalendar gc = new GregorianCalendar();
    gc.setTimeInMillis(date.getTime());
    XMLGregorianCalendar xgc;
    xgc = DatatypeFactory.newInstance().newXMLGregorianCalendar(gc);
    return xgc;
  }

  /**
   * 灏嗕竴涓獃yyy-MM-dd鐨勬椂闂村瓧绗︿覆杞寲涓篨MLGregorianCalendar
   * @param time
   * @return
   */

  /**
   * XML鏃堕棿杞寲涓篋ate
   * @param xgc
   * @return
   */
  public static Date parseDate(XMLGregorianCalendar xgc)
  {
    return xgc.toGregorianCalendar().getTime();
  }

  /**
   * 鑾峰彇涓や釜鏃堕棿闂撮殧鍑犲ぉ
   * 
   * @param fromDate
   * @param toDate
   * @return
   */
  public static int getInterval(Date fromDate, Date toDate)
  {
    long interval = toDate.getTime() - fromDate.getTime();
    return (int) (interval / (24 * 3600000));
  }


  /**
   * 姣旇緝涓や釜鏃堕棿
   *      濡傛灉鐩哥瓑锛岃繑鍥炲�涓�
   *      濡傛灉鍓嶄竴涓椂闂存瘮杈冩棭锛岃繑鍥炲�<0
   *      濡傛灉鍓嶄竴涓椂闂存瘮杈冩櫄锛岃繑鍥炲�>0
   * @param firstDate
   * @param secondDate
   * @return
   */
  public static int compareDate(Date firstDate, Date secondDate)
  {
    Calendar firstCalendar = new GregorianCalendar();
    firstCalendar.setTime(firstDate);

    Calendar secondCalendar = new GregorianCalendar();
    secondCalendar.setTime(secondDate);

    return firstCalendar.compareTo(secondCalendar);
  }


  /**
   * Date鏃ユ湡鏍煎紡鍖栦箣鍚庡啀娆¤浆鍖栦负Date锛堝彲鍦ㄩ渶瑕佹敼鍙楧ate鎼哄甫鐨勪俊鎭椂浣跨敤锛屽甯屾湜鎶婁竴涓�yyyy-MM-dd'鐨勬棩鏈熷彉涓哄彧鏈夊勾淇℃伅'yyyy'鐨勬棩鏈燂級
   * @param date
   * @param pattern
   * @return
   * @throws ParseException
   */


  /**
   * 鍒ゆ柇涓や釜鏃ユ湡鏄惁鍦ㄥ悓涓�ぉ
   * @param preDate
   * @param currDate
   * @return
   * @throws ParseException
   */

  public static void main(String[] args)
      throws ParseException
  {


  }
}
